import 'dart:math';

import 'package:flutter/material.dart';

class SoundLevelPainter extends CustomPainter {
  final double level;

  SoundLevelPainter(this.level);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.fill;

    final maxBars = 30;
    final barWidth = size.width / maxBars;
    final maxHeight = size.height;

    for (var i = 0; i < maxBars; i++) {
      // 使用正弦函数创建波浪效果
      final amplitude = (level * maxHeight / 2) * 
          (1 + sin(i / 2 + DateTime.now().millisecondsSinceEpoch / 100));
      
      final height = amplitude.clamp(5.0, maxHeight);
      
      canvas.drawRRect(
        RRect.fromRectAndRadius(
          Rect.fromLTWH(
            i * barWidth,
            maxHeight - height,
            barWidth - 2,
            height,
          ),
          Radius.circular(4),
        ),
        paint,
      );
    }
  }

  @override
  bool shouldRepaint(SoundLevelPainter oldDelegate) => true;
} 